<template>
 
    <el-dialog :title="info.isAdd ? '角色添加' : '角色编辑'" :visible.sync="info.isshow">

      <el-form :model="user">
      
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>
           
        <el-form-item label="角色权限" label-width="120px">
          <!-- 树形控件 -->
           <el-tree
            :data="menuList"
            show-checkbox
            node-key="id"
            :props="{ children: 'children', label: 'title' }"
            ref="eltree"
            >
          </el-tree>
        
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>

    </el-dialog>

</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {IndexRoutes} from "../../../router";
import { reqroleadd, reqroleedit, reqroleinfo, reqmenulist } from "../../../https/api";
import { successAlert } from "../../../utils/alert";
export default { 
  props: ["list","info"],
  data() {
    return {
    user:{
          rolename:"",
          menus:"",
          status:2
          },
      menuList:[],
      IndexRoutes,  // 二级路由
    };
  },
  computed: {
    ...mapGetters({
       userInfo:"userInfo"
    }),
  },
  methods: {
    ...mapActions({
       changeUserInfo:"changeUserInfo"
    }),
    // 取消按钮
    cancel(){
        this.info.isshow = false;
    },
    // 清空user
    empty(){
        this.user ={
            rolename:"",
            status:2
        };
        // 清空树形控件
        this.$refs.eltree.setCheckedKeys([]);
    },
    // 添加按钮
    add(){
        this.user.menus = JSON.stringify(this.$refs.eltree.getCheckedKeys())
        reqroleadd(this.user).then( (res) =>{
          // console.log(res);
            if( res.data.code === 200 ){
                successAlert(res.data.msg);
                //  清空user数据
                this.empty();
                 // 弹框消失
              this.info.isshow  = false;
                 // 刷新列表  添加成功后通知父组件刷新列表
              this.$emit("init")
           }
        });
    },
    //修改pid
    changePid(){
    // 如果pid是0 type=1 否则type = 2 
        if(this.user.pid == 0){
            this.user.type = 1
        }else{
            this.user.type = 2
        }
    },
    // 获取一条的数据
    getOne(id){
        reqroleinfo( {id:id} ).then(res=>{
          // console.log(1);
            if(res.data.code==200){
                this.user = res.data.list
                // 补id
                this.user.id = id;
                // 给树形控件赋值
                this.$refs.eltree.setCheckedKeys(JSON.parse(this.user.menus));
            }
        })
    },
    // 修改
    update(){

        //  先取出树的数据给user.menus  再发请求
        this.user.menus = JSON.stringify(this.$refs.eltree.getCheckedKeys());

        reqroleedit(this.user).then(res=>{
          // console.log(res);
            if(res.data.code == 200){
                successAlert(res.data.msg);
                // 判断要修改的用户id 是不是当前的用户id 是自动退出 不是 继续下面的操作
                if(this.user.id === this.userInfo.roleid){
                  this.changeUserInfo({});
                  this.$router.replace("/login");
                  return;
                }

                // 清空user的数据
                this.empty();
                // 弹框消失
                this.info.isshow = false;
                // 刷新列表
                this.$emit("init")
            }
        })
    },
    closed(){
        //如果是编辑的关闭，就要执行empty
        if(!this.info.isAdd){
            this.empty()
        }
    }
  },
  mounted() {
    // 挂载完成请求菜单列表
    reqmenulist( {istree:true} ).then( (res) =>{
      // console.log(res);
      if(res.data.code == 200){
        this.menuList = res.data.list;
      }
    })
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
</style>